<?php
/**
 *
 */
class FacultiesController extends AppController {

	//public $uses = array('Group', 'User');

	public $helpers = array('Html', 'Form');

	public function isAuthorized($user) {
		// Users can: Nothing
		if (in_array($this -> action, array())) {
			return true;
		}

		// Show only to administrators
		if (in_array($this -> action, array('index', 'add', 'edit', 'delete')))
			if ($this -> Auth -> loggedIn() && $this -> Auth -> user('rights') !== 'admin')
				return false;

		return parent::isAuthorized($user);
	}

	/**
	 * It creates a variable named faculties in which there are all'the faculties
	 */
	public function index() {
		$this -> set('faculties', $this -> Faculty -> find('all'));
		$this -> layout = 'admin';
	}

	/**
	 * Add a new faculty to the database
	 */
	public function add() {
		if ($this -> request -> is('post')) {
			if ($this -> Faculty -> save($this -> request -> data)) {
				$this -> Session -> setFlash('New faculty has been created successfully.');
				$this -> redirect(array('action' => 'index'));
			} else {
				$this -> Session -> setFlash('Unable to add a new faculty.');
			}
		}
		$this -> layout = 'admin';
	}

	/**
	 * Edit the name of a faculty
	 */
	public function edit($id = null) {
		$this -> Faculty -> id = $id;
		if ($this -> request -> is('get')) {
			$this -> request -> data = $this -> Faculty -> read();
		} else {
			if ($this -> Faculty -> save($this -> request -> data)) {
				$this -> Session -> setFlash('The information have been updated.');
				$this -> redirect(array('action' => 'index'));
			} else {
				$this -> Session -> setFlash('Unable to update the information.');
			}
		}
		$this -> layout = 'admin';
	}

	/**
	 * Delete an user from the databse
	 */
	public function delete($id = null) {
		if ($this -> request -> is('get')) {
			throw new MethodNotAllowedException();
		}
		if ($this -> Faculty -> delete($id)) {
			$this -> Session -> setFlash('Faculty successfully deleted.');
			$this -> redirect(array('action' => 'index'));
		}
		$this -> layout = 'admin';
	}

}
